package com.dj.ms.event.support;


import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <br>
 * <p>重试任务数据管理器</p>
 *
 * <br>
 * @author ZJG
 * @version 1.0
 * @date 2021/7/27 上午9:47
 */
@Slf4j
public class EventRetryTaskDataManager {

    private static final Map<String, EventRetryTaskRepository> repositoryMap = Maps.newLinkedHashMap();

    /**
     * 注册重试任务数据源
     * @param repository
     */
    public static void register(String key, EventRetryTaskRepository repository) {
        log.info("EventRetryTaskDataManager:register:repository:{}", repository);
        if (StringUtils.isBlank(key) || Objects.isNull(repository)) {
            return;
        }
        repositoryMap.put(key, repository);
    }

    public static EventRetryTaskRepository getRepository(String key) {
        if (!repositoryMap.containsKey(key)) {
            return null;
        }
        return repositoryMap.get(key);
    }

    public static List<EventRetryTaskRepository> getAllRepository() {
        if (CollectionUtils.isEmpty(repositoryMap)) {
            return null;
        }
        return repositoryMap.values().stream().collect(Collectors.toList());
    }

}
